package com.rxyb.authority.utils;

import com.rxyb.authority.gateway.dto.menu.AuthorityMenuVo;

import java.util.ArrayList;
import java.util.List;

/**
 * List父子节点构造树形
 *
 * @author duyh
 * @date 2020/6/8
 */
public class TreeUtils {

    //将list集合转成树形结构的list集合
    public static List<AuthorityMenuVo> listToTree(List<AuthorityMenuVo> list) {
        //用递归找子。
        List<AuthorityMenuVo> treeList = new ArrayList<AuthorityMenuVo>();
        for (AuthorityMenuVo tree : list) {
            if (tree.getMenuParentId().equals("0")) {
                treeList.add(findChildren(tree, list));
            }
        }
        return treeList;
    }

    //寻找子节点
    private static AuthorityMenuVo findChildren(AuthorityMenuVo tree, List<AuthorityMenuVo> list) {
        for (AuthorityMenuVo node : list) {
            if (node.getMenuParentId().equals(tree.getMenuId())) {
                if (tree.getSubMenu() == null) {
                    tree.setSubMenu(new ArrayList<AuthorityMenuVo>());
                }
                tree.getSubMenu().add(findChildren(node, list));
            }
        }
        return tree;
    }

}
